<template>
	<view>
		<u--form labelPosition="left" :model="model1" :rules="rules" ref="uForm">
			<u-form-item label="标题" prop="userInfo.title" borderBottom>
				<u--input v-model="model1.userInfo.name" border="none" placeholder="请输入标题"></u--input>
			</u-form-item>
			<u-form-item label="体重" prop="userInfo.weight" borderBottom>
				<u--input v-model="model1.userInfo.weight" border="none" placeholder="请输入现体重,仅限输入数字,单位为千克"></u--input>
			</u-form-item>
			<u-form-item label="照片" prop="userInfo.image" borderBottom>
				<u-upload
						:fileList="fileList1"
						@afterRead="afterRead"
						:capture="['camera','album']"
						@delete="deletePic"
						name="1"
						multiple
						:maxCount="1"
					></u-upload>
			</u-form-item>
		</u--form>
		<view style="margin-top: 50rpx;">
			<u-button type="primary" size="large" text="提交" @click="submit()"></u-button>
		</view>			
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ImageSrc:'',
				ImageFile:'',
				fileList1:[],
				model1: {
					userInfo: {
						title: '',
						weight: '',
					},
				},
				rules: {
					'userInfo.title': {
						type: 'string',
						required: true,
						message: '请填写标题',
						trigger: ['blur', 'change']
					},
					'userInfo.weight': {
						type: 'string',
						max: 3,
						required: true,
						message: '请输入体重',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		methods: {
			submit(){
				 // 创建一个空对象
				let formData = {};

				// 添加表单字段
				formData.file = this.ImageFile;
				formData.age = '25';
			},
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					this.ImageFile = lists[i]
						let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return 
			},
			
		},
		onReady() {
				//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
		    	this.$refs.uForm.setRules(this.rules)
		    },
	}
</script>

<style>

</style>
